import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/Layout/index.vue'
import Login from '@/views/Login/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import Detail from '@/views/Detail/index.vue'

// createRouter:创建router实例对象
// createWebHistory:创建history模式的路由
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'layout',
            component: Layout,
            // 添加二级路由
            children: [
                {
                    path: '', // 置空, 当访问 / 时 , home 组件显示
                    name: 'home',
                    component: Home
                },
                {
                    path: '/category/:id', // 占位id参数
                    name: 'category',
                    component: Category
                },
                {
                    path: '/category/sub/:id', // 占位id参数
                    name: 'subCategory',
                    component: SubCategory
                },
                {
                    path: '/detail/:id',
                    name: 'detail',
                    component: Detail,
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: Login
        }
    ],
    /* 配置这个路由行为，就可以实现点击一级路由时，自动跳转到浏览器最顶部 */
    scrollBehavior() {
        return {top: 0}
    }
})

export default router
